package com.itheima.order.service;

import com.itheima.feign.client.UserClient;
import com.itheima.feign.domain.User;
import com.itheima.order.domain.Order;
import com.itheima.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    UserClient userClient;

    public Order findOrderByUserId(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.selectById(orderId);

        //2.通过用户id，远程调用用户服务，查询用户对象
//        //方式1： 试用1restTemper写固定地址
//        String url = "http://localhost:8081/user/"+order.getUserId();
//        User user = restTemplate.getForObject(url, User.class);

        //方式2：试用restTemper负载均衡调用
//        String url = "http://userservice/user/"+order.getUserId();
//        User user = restTemplate.getForObject(url, User.class);

        //方式3：使用feign远程调用
        User user = userClient.findById(order.getUserId());

        //3.讲返回的用户对象，设置给order对象
        order.setUser(user);
        // 4.返回
        return order;
    }
}
